# -*- coding:utf-8 -*-
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Template, Context
import StringIO
from PIL import Image

def index(request):
    t=get_template('index.htm')
    html=t.render(Context({}))
    return HttpResponse(html)

def test(request):
    t=get_template('test.htm')
    html=t.render(Context({}))
    return HttpResponse(html)

def upload(request):
    buf = request.FILES.get('photo', None)
    data = buf.read()
    f = StringIO.StringIO(data)
    image = Image.open(f)
    image = image.convert('RGB')
    name = '%s%s' % ('media/upload/', buf.name)
    image.save(file(name, 'wb'), 'PNG')
    return HttpResponse(f.getvalue(),"image/jpg")